package ormg

import (
	"beergo/lib/mysql"
	"beergo/lib/tool"
	"github.com/beego/beego/v2/server/web/context"
	"gorm.io/gorm"
	"reflect"
)

type BaseModel struct {
	gorm.Model
	qb  *mysql.QueryBuilder
	ctx *context.Context
}

func (this *BaseModel) SetCtx(ctx *context.Context) *BaseModel {
	this.ctx = ctx
	return this
}
func (this *BaseModel) GetCtx() *context.Context {
	return this.ctx
}

func (this *BaseModel) SetQueryBuilder(qb *mysql.QueryBuilder) *BaseModel {
	this.qb = qb
	return this
}
func (this *BaseModel) GetQueryBuilder() *mysql.QueryBuilder {
	return this.qb
}

func (this BaseModel) CheckOrmGroup(findIn string, find string) bool {
	return tool.HasSameWord(findIn, find)
}

func (this BaseModel) GetOrmGroups(model interface{}, field string) string {
	filed, find := reflect.TypeOf(model).FieldByName(field)
	if find {
		return filed.Tag.Get("groups")
	}
	return ""
}

func (this BaseModel) LogOut(info interface{}) {
	//err := logs.GetLogger("INFO").Output(1, fmt.Sprintln(info))
	//if err != nil {
	//	this.LogOut(err)
	//}
}

func (this BaseModel) GetOrm() orm.Ormer {
	o := orm.NewOrm()
	return o
}